{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 4.15 \u987a\u5e8f\u8fed\u4ee3\u5408\u5e76\u540e\u7684\u6392\u5e8f\u8fed\u4ee3\u5bf9\u8c61\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u6709\u4e00\u7cfb\u5217\u6392\u5e8f\u5e8f\u5217\uff0c\u60f3\u5c06\u5b83\u4eec\u5408\u5e76\u540e\u5f97\u5230\u4e00\u4e2a\u6392\u5e8f\u5e8f\u5217\u5e76\u5728\u4e0a\u9762\u8fed\u4ee3\u904d\u5386\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "heapq.merge() \u51fd\u6570\u53ef\u4ee5\u5e2e\u4f60\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import heapq\na = [1, 4, 7, 10]\nb = [2, 5, 6, 11]\nfor c in heapq.merge(a, b):\n    print(c)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "heapq.merge \u53ef\u8fed\u4ee3\u7279\u6027\u610f\u5473\u7740\u5b83\u4e0d\u4f1a\u7acb\u9a6c\u8bfb\u53d6\u6240\u6709\u5e8f\u5217\u3002\n\u8fd9\u5c31\u610f\u5473\u7740\u4f60\u53ef\u4ee5\u5728\u975e\u5e38\u957f\u7684\u5e8f\u5217\u4e2d\u4f7f\u7528\u5b83\uff0c\u800c\u4e0d\u4f1a\u6709\u592a\u5927\u7684\u5f00\u9500\u3002\n\u6bd4\u5982\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\u6765\u6f14\u793a\u5982\u4f55\u5408\u5e76\u4e24\u4e2a\u6392\u5e8f\u6587\u4ef6\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "with open('sorted_file_1', 'rt') as file1, \\\n    open('sorted_file_2', 'rt') as file2, \\\n    open('merged_file', 'wt') as outf:\n\n    for line in heapq.merge(file1, file2):\n        outf.write(line)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6709\u4e00\u70b9\u8981\u5f3a\u8c03\u7684\u662f heapq.merge() \u9700\u8981\u6240\u6709\u8f93\u5165\u5e8f\u5217\u5fc5\u987b\u662f\u6392\u8fc7\u5e8f\u7684\u3002\n\u7279\u522b\u7684\uff0c\u5b83\u5e76\u4e0d\u4f1a\u9884\u5148\u8bfb\u53d6\u6240\u6709\u6570\u636e\u5230\u5806\u6808\u4e2d\u6216\u8005\u9884\u5148\u6392\u5e8f\uff0c\u4e5f\u4e0d\u4f1a\u5bf9\u8f93\u5165\u505a\u4efb\u4f55\u7684\u6392\u5e8f\u68c0\u6d4b\u3002\n\u5b83\u4ec5\u4ec5\u662f\u68c0\u67e5\u6240\u6709\u5e8f\u5217\u7684\u5f00\u59cb\u90e8\u5206\u5e76\u8fd4\u56de\u6700\u5c0f\u7684\u90a3\u4e2a\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u4e00\u76f4\u4f1a\u6301\u7eed\u76f4\u5230\u6240\u6709\u8f93\u5165\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u90fd\u88ab\u904d\u5386\u5b8c\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}